<template>
  <div class="kfc-tab">
    <a-spin :spinning="loading">
      <div class="content-card">
        <h2 class="title">KFC文学</h2>
        <div class="kfc-content" v-if="content">
          <p class="content-text">{{ content }}</p>
        </div>
        <div class="error-message" v-else-if="error">
          <p>{{ error }}</p>
        </div>
        <div class="empty-content" v-else>
          <p>暂无内容</p>
        </div>
      </div>
    </a-spin>
  </div>
</template>

<script setup lang="ts">
import { message } from 'ant-design-vue';
import { onMounted, ref, toRefs, watch } from 'vue';
import { API_BASE_URL } from '../utils/WebConstants.ts';

const props = defineProps<{
  refreshKey?: number;
}>();
const { refreshKey } = toRefs(props);

const loading = ref(true);
const error = ref<string | null>(null);
const content = ref<string | null>(null);

const fetchKFC = async () => {
  try {
    loading.value = true;
    error.value = null;
    const response = await fetch(`${API_BASE_URL}/kfc?encoding=text`);
    const result = await response.text();

    if (response.ok && result) {
      content.value = result;
    } else {
      content.value = result || '获取内容失败';
    }
  } catch (err) {
    error.value = '获取KFC文学失败';
    message.error('获取KFC文学失败，请稍后重试');
    console.error('获取KFC文学失败:', err);
  } finally {
    loading.value = false;
  }
};

// 监听refreshKey变化以刷新数据
watch(refreshKey, () => {
  fetchKFC();
});

onMounted(() => {
  fetchKFC();
});
</script>

<style scoped>
.kfc-tab {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
}

.content-card {
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  border-radius: 12px;
  padding: 2rem;
  width: 800px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.title {
  text-align: center;
  font-size: 2rem;
  margin-bottom: 1.5rem;
  color: #fff;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

.kfc-content {
  text-align: justify;
  font-size: 1.2rem;
  line-height: 1.8;
  color: #fff;
  padding: 1rem;
  min-height: 200px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.content-text {
  margin: 0;
  text-align: justify;
}

.error-message,
.empty-content {
  text-align: center;
  padding: 2rem;
  color: #ccc;
}

@media (max-width: 768px) {
  .content-card {
    padding: 1rem;
    margin: 0 0.5rem;
    width: calc(100% - 1rem);
  }

  .title {
    font-size: 1.5rem;
  }

  .kfc-content {
    font-size: 1rem;
    padding: 0.5rem;
  }
}
</style>
